/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.conTableFornecedor;
import br.com.fatecpg.zanotti.acessobanco.dmlFornecedor;
import br.com.fatecpg.zanotti.jframes.cadastros.cadFornecJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Fornecedor;

/**
 *
 * @author Luiz Zanotti
 */
public class conFornecJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadProdutoJInternalFrame */
    public conFornecJInternalFrame() {
        initComponents();
        conFornecedorjTable.getColumnModel().getColumn(1).setMaxWidth(90);
        conFornecedorjTable.getColumnModel().getColumn(1).setMinWidth(90);
        conFornecedorjTable.getColumnModel().getColumn(0).setMaxWidth(300);
        conFornecedorjTable.getColumnModel().getColumn(0).setMinWidth(210);
    }

    public void atualizaTable() {
        conFornecedorjTable.removeAll();
        conTableFornecedor con = new conTableFornecedor();
        conFornecedorjTable = con.ConsultaTabelaToda(conFornecedorjTable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conFornecedorjTable = new javax.swing.JTable();
        buscarjTextField = new javax.swing.JTextField();
        nome_cpfjComboBox = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();
        pesquisarjLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Fornecedores");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        conFornecedorjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nome", "Código"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        conFornecedorjTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(conFornecedorjTable);
        conFornecedorjTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        nome_cpfjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CNPJ" }));

        buscarjButton.setText("Buscar");
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });

        pesquisarjLabel.setText("Pesquisar por:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(pesquisarjLabel)
                        .addGap(6, 6, 6)
                        .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarjButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarjLabel)
                    .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarjButton))
                .addContainerGap())
        );

        alterarjButton.setText("Alterar");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        excluirjButton.setText("Excluir");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });

        jButton2.setText("Novo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                    .addComponent(excluirjButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                    .addComponent(alterarjButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(excluirjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(128, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        conFornecedorjTable.removeAll();

        if (nome_cpfjComboBox.getSelectedItem() == "Nome") {
            conTableFornecedor con = new conTableFornecedor();
            conFornecedorjTable = con.ConsultaTabelaPorNome(conFornecedorjTable, buscarjTextField.getText());
        } else {

            conTableFornecedor con = new conTableFornecedor();
            conFornecedorjTable = con.ConsultaTabelaPorCNPJ(conFornecedorjTable, buscarjTextField.getText());
        }
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
      if(conFornecedorjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{

        String str = conFornecedorjTable.getValueAt(conFornecedorjTable.getSelectedRow(), 1).toString();
        dmlFornecedor.deletar(str);
        atualizaTable();
      }
    }//GEN-LAST:event_excluirjButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
        cadFornecJInternalFrame.getInstancia().setVisible(true);
        cadFornecJInternalFrame.getInstancia().limpaTudo();
    }//GEN-LAST:event_jButton2ActionPerformed
    Fornecedor forn;
    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
      if(conFornecedorjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{

        this.setVisible(false);
        String str = conFornecedorjTable.getValueAt(conFornecedorjTable.getSelectedRow(), 1).toString();

        forn = new conTableFornecedor().ConsultaObjetoPorLinha(str);

        cadFornecJInternalFrame.getInstancia().setVisible(true);

        cadFornecJInternalFrame.getInstancia().setEmailjTextField(forn.getNMEMAIL());
        cadFornecJInternalFrame.getInstancia().setEndjTextArea(forn.getNMENDERECO());
        cadFornecJInternalFrame.getInstancia().setNomejTextField(forn.getNMFORNECEDOR());
        cadFornecJInternalFrame.getInstancia().setCodjTextField(forn.getCDFORNECEDOR());
        cadFornecJInternalFrame.getInstancia().setCnpjjFormattedTextField(forn.getCDCNPJFORNECEDOR());
        cadFornecJInternalFrame.getInstancia().setTelefonejFormattedTextField(forn.getCDTELEFONE());


        cadFornecJInternalFrame.getInstancia().setTitle("Alteração de Cadastro de Cliente");
        cadFornecJInternalFrame.getInstancia().setCadastrarjButton("Salvar");
      }
    }//GEN-LAST:event_alterarjButtonActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened
    private static conFornecJInternalFrame p;

    public static conFornecJInternalFrame getInstancia() {
        if (p == null) {
            p = new conFornecJInternalFrame();
        }
        return p;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JTextField buscarjTextField;
    private javax.swing.JTable conFornecedorjTable;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox nome_cpfjComboBox;
    private javax.swing.JLabel pesquisarjLabel;
    // End of variables declaration//GEN-END:variables
}
